package com.sky.seekbar;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.text.DecimalFormat;

public class DisplayUtil
{
    private static class SingletonHolder
    {
        private static final DisplayUtil INSTANCE = new DisplayUtil();
    }
    private DisplayUtil(){}
    public static final DisplayUtil getInstance()
    {
        return SingletonHolder.INSTANCE;
    }

    public static int px2dip(Context context, float pxValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dip2px(Context context, float dipValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


    public static int px2sp(Context context, float pxValue)
    {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int sp2px(Context context, float spValue)
    {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static Uri getURIFromResrouceID(Context context, int resId)
    {
        Resources resources = context.getResources();
        Uri u = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + resources.getResourcePackageName(resId) + '/'
                + resources.getResourceTypeName(resId) + '/'
                + resources.getResourceEntryName(resId));
        return u;
    }

    /**
     * 保留两位小数
     * @param data
     * @return
     */
    public static String decimalFormat2(double data)
    {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(data);
    }

    /**
     * 去掉小数
     * @param data
     * @return
     */
    public static String decimalFormat(double data)
    {
        DecimalFormat df = new DecimalFormat("#");
        return df.format(data);
    }



    /**
     * 保留一位小数
     * @param data
     * @return
     */
    public static String decimalFormat1(double data)
    {
        if (data == 0)
        {
            return "0.0";
        }
        DecimalFormat df = new DecimalFormat("#.0");
        return df.format(data);
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s){
        if(s.isEmpty())
        {
            return "";
        }
        if(s.indexOf(".") > 0){
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    /**
     * 人民币显示方式
     * @param data
     * @return
     */
    public static String decimalFormatMoney(double data)
    {
        DecimalFormat df = new DecimalFormat("#,##0.00");
        return df.format(data);
    }

    /**
     * 是否为0-9的数字
     * @param value
     * @return
     */
    public static boolean isNum(String value)
    {
        if(value.isEmpty())
        {
            return false;
        }
        boolean flag = false;
        char[] result = value.toCharArray();
        String num = "0123456789";
        for (int i = 0; i < result.length;i++)
        {
            char str = result[i];
            flag = num.indexOf(str) == -1?false:true;
            if(!flag)
            {
                break;
            }
        }
        return flag;
    }

    /**
     * 判断某个字符出现了几次
     * @param str
     * @param key
     * @return
     */
    public static int countStr(String str, String key)
    {
        int count = 0;
        for (int i = 0; i < str.length(); i++)
        {
            String data = String.valueOf(str.charAt(i));
            if (data.equals(key))
                count++;
        }
        return count == 0 ? -1 : count;
    }

    /**
     * 判断小数点后有几位小数
     * @param value
     * @return
     */
    public static int countNumAfterPoint(String value)
    {
        int num = 0;
        if(!value.isEmpty())
        {
            String[] valueArray = value.split("\\.");
            if(valueArray.length > 1)
            {
                num = valueArray[1].length();
            }
        }
        return num;
    }

    /**
     * 是否为0-9的浮点型数字
     * @param value
     * @return
     */
    public static boolean isFloatNum(String value)
    {
        if(value.isEmpty())
        {
            return false;
        }
        if(countStr(value,".") > 1)
        {
            return false;
        }
        boolean flag = false;
        char[] result = value.toCharArray();
        String num = "0123456789.";
        for (int i = 0; i < result.length;i++)
        {
            char str = result[i];
            flag = num.indexOf(str) == -1?false:true;
            if(!flag)
            {
                break;
            }
        }
        return flag;
    }

    /**
     * 获得屏幕高度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context)
    {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context)
    {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    public static void main(String[] args) throws Exception
    {
//        System.out.println(decimalFormat2(2.0));
//        System.out.println(decimalFormatMoney(778979879879.0));
        System.out.println("1..2 is float"+isFloatNum("1..2"));
        System.out.println("1.2. is float"+isFloatNum("1.2."));
        System.out.println("1.2..3 is float"+isFloatNum("1.2..3"));
        System.out.println("1.2.5 is float"+isFloatNum("1.2.5"));
        System.out.println("1.2 is float"+isFloatNum("1.2"));
    }
}
